/**
 * 一、将下面异步代码使用Promise的方式改进
 */

// setTimeout(function () {
//     var a = 'hello'
//     setTimeout(function () {
//     var b = 'lagou'
//     }, 10);setTimeout(function () {
//         var c = 'I ❤ U'
//         console.log(a + b + c);
//     }, 10);
// }, 10);

let obj = {
    a: "hello",
    b: "lagou",
    c: "I❤U",
};

function getPromise(val) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(val);
        }, 10);
    });
}

for (const key in obj) {
    window[key] = getPromise(obj[key]);
}

let arr = Object.keys(obj);
let promiseArr = arr.map(val => window[val]);
Promise.all(promiseArr).then(data => {
    let result = data.join(" ");
    console.log(result);
});

/**
 * 二、基于以下代码完成下面的四个练习
 */

const fp = require("lodash/fp");

const cars = [
    {
        name: "Ferrari FF",
        horsepower: 660,
        dollar_value: 700000,
        in_stock: true,
    },
    {
        name: "Spyker C12 Zagato",
        horsepower: 650,
        dollar_value: 648000,
        in_stock: false,
    },
    {
        name: "Audi R8",
        horsepower: 525,
        dollar_value: 114200,
        in_stock: false,
    },
    {
        name: "Aston Martin One-77",
        horsepower: 750,
        dollar_value: 1850000,
        in_stock: true,
    },
    {
        name: "Pagani Huayra",
        horsepower: 700,
        dollar_value: 1300000,
        in_stock: false,
    },
];

// 练习1： 使用函数组合 fp.flowRight() 重新实现下面这个函数
// let isLastInStock = function(cars) {
//     // 获取最后一条数据
//     let last_car = fp.last(cars)
//     // 获取最后一条数据的 in_stock 属性值
//     return fp.prop('in_stock', last_car)
// }

let isLastInStock = function (cars) {
    let fn = fp.flowRight(fp.prop("in_stock"), fp.last);
    return fn(cars);
};

console.log(isLastInStock(cars));

//练习2： 使用fp.flowRight()、fp.prop()、fp.first()获取第一个cat的name
let getFirstName = function (cars) {
    let fn = fp.flowRight(fp.prop("name"), fp.first);
    return fn(cars);
};

console.log(getFirstName(cars));

// 练习3： 使用帮助函数_average 重构averageDollarValue,使用函数组合的方式实现
let _average = function (xs) {
    return fp.reduce(fp.add, 0, xs) / xs.length;
};

// let averageDollarValue = function (cars) {
//     let dollar_values = fp.map(function(car) {
//         return car.dollar_value
//     }, cars)
//     return _average(dollar_values)
// }

let averageDollarValue = fp.flowRight(
    _average,
    fp.map(v => v.dollar_value)
);

console.log(averageDollarValue(cars));

//练习4：使用flowRight写一个sanitizeNames()函数，返回一个下划线连接的小写字符串，把数组中的name转换为这种形式
// sanitizeNames(["Hello Word"]) => ["hello_word"]
let _underscore = fp.replace(/\W+/g, "_");

let fnSon = fp.flowRight(_underscore, fp.toLower);
let fn = fp.map(v => fnSon(v));
console.log(fn(["Hello Word"]));

/**
 * 三、基于下面提供的代码，完成后续的四个练习
 */

//  练习1：使用fp.add(x,y)和fp.map(f,x)创建一个能让functor里的值增加的函数ex1

const { MayBe, Container } = require("./support");

let maybe = MayBe.of([5, 6, 1]);
let ex1 = () => {
    let add = fp.map(v => fp.add(v, 1));
    return maybe.map(v => add()(v));
};
console.log(ex1());

// 练习2: 实现一个函数ex2,能够使用fp.first获取列表的第一个元素
let xs = Container.of(["do", "ray", "me", "fa", "so", "la", "ti", "do"]);
let ex2 = () => {
    return xs.map(v => fp.first(v));
};
console.log(ex2());

// 练习3：实现一个函数ex3，使用safeProp和fp.first找到user的名字的首字母
let safeProp = fp.curry(function (x, o) {
    return MayBe.of(o[x]);
});
let user = { id: 2, name: "Albert" };
let ex3 = () => {
    let getFirstName = fp.curry((o) => {return safeProp('name', o).map(v => fp.first(v))})
    return getFirstName
};
console.log(ex3()(user));

// 练习4：使用MayBe重写ex4，不要有if语句
let ex4 = function (n) {
    // if (n) {
    //     return parseInt(n)
    // }
    return MayBe.of(n).map(v => parseInt(v))._value
}
console.log(ex4(4.4));